package com.liang.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liang.entity.CBlog;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author 李昂
 * @since 2020-11-30
 */
public interface CBlogMapper extends BaseMapper<CBlog> {

    //前台查看blog
//    @Select("select bid,id,type,title from c_blog where id=#{id}")
    List<CBlog> getBlog(@Param("id") String id, @Param("type") String type);

    @Select("select container from c_blog where bid=#{bid}")
    String getContainer(@Param("bid") int bid);

    @Update("update c_blog set title=#{title} ,container=#{container},type=#{type} where bid=#{bid}")
    boolean modifyBlog(@Param("bid") int bid, @Param("title") String title, @Param("container") String container, @Param("type") String type);

    @Update("update c_blog set lookTotal=lookTotal+1 where bid=#{bid} and lookTotal=#{total}")
    boolean lookBlog(@Param("bid") int bid, @Param("total") int total);

    @Insert("insert into bid_id values(#{bid},#{id})")
    boolean insertPraise(@Param("bid") Integer bid, @Param("id") String id);

    @Delete("delete from bid_id where bid = #{bid}")
    boolean deletePraise(@Param("bid") Integer bid);

    @Select("select count(*) from bid_id where bid=#{bid} and id=#{id}")
    int getPraise(@Param("bid") int bid, @Param("id") String id);

    @Select("select lookTotal from c_blog where bid=#{bid}")
    int lookTotal(@Param("bid") int bid);

    @Update("update c_blog set praise=praise+1 where bid=#{bid} and praise=#{total}")
    boolean Praise(@Param("bid") int bid, @Param("total") int total);

    @Select("select praise from c_blog where bid=#{bid}")
    int praiseTotal(@Param("bid") int bid);

    @Update("update c_blog set commentTotal=commentTotal+1 where bid=#{bid} and commentTotal=#{total}")
    boolean comment(@Param("bid") int bid, @Param("total") int total);

    @Select("select commentTotal from c_blog where bid=#{bid}")
    int commentTotal(@Param("bid") int bid);

    //备份历史blog数据  ----定时任务
    @Insert("INSERT INTO c_recordblogdata(id,lookTotal,praise,commentTotal,datetime) select id ,SUM(lookTotal),SUM(praise),SUM(commentTotal),NOW() from c_blog GROUP BY id")
    boolean produceBlog();

    //删除超出七天的历史数据 ----定时任务
    @Delete("DELETE from c_recordblogdata where TO_DAYS(NOW())-TO_DAYS(datetime)>=8")
    boolean deleteProduce();

    //查看7天用户blog数据访问量
    @Select("select lookTotal,praise,commentTotal,datetime from c_recordblogdata where id=#{id}")
    List<CBlog> getHistoryBlogData(@Param("id") String id);

    //查看blog类型的观看数，评论数，点赞
    @Select("select type,SUM(lookTotal) as lookTotal,SUM(praise) as praise,SUM(commentTotal) as commentTotal from c_blog WHERE id=#{id} group by type")
    List<CBlog> getBlogType(@Param("id") String id);


    //------------游客可查看的blog页面---------------

    /**
     * <p>
     * 查询 : 根据state状态查询用户列表，分页显示
     * </p>
     *
     * @param page  分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位(你可以继承Page实现自己的分页对象)
     * @param state 状态
     * @return 分页对象
     */
    IPage<CBlog> selectPageBlog(Page<?> page, Integer state);

    //blogs搜索
    List<CBlog> BlogSearch(@Param("title") String title);

}
